package com.lysj.admin.master.bussiness.admin.service;

import com.lysj.admin.master.bussiness.admin.domain.CompanyProviderBind;
import com.lysj.admin.master.bussiness.admin.mapper.CompanyProviderBindMapper;
import com.lysj.admin.master.common.exception.BaseException;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import com.lysj.admin.master.common.web.ParamUtil;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.SelectItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author Created by drj on 2020-01-03 16:25
 * @description
 */
@Slf4j
@Service
@Transactional
public class CompanyProviderBindService extends MyService<CompanyProviderBindMapper, CompanyProviderBind> {

    @Resource
    private ServiceProviderService serviceProviderService;

    //获取当前用工平台已关联的服务商
    public List<String> findByCompanyIdSelectItem(String companyId) {
        return this.lambdaQuery()
                .select(CompanyProviderBind::getServiceProviderId)
                .eq(CompanyProviderBind::getCompanyId, companyId)
                .list()
                .stream()
                .map(e -> {
                    return new String(e.getServiceProviderId());
                })
                .collect(Collectors.toList());
    }

    //获取用工平台未关联服务商selectItem
    public List<SelectItem<String, String>> leftSelectItem(String companyId) {
        //所有服务商的selectItem
        List<SelectItem<String, String>> selectItemSer = serviceProviderService.selectItem();
        //获取当前用工平台已关联的服务商
        List<String> companyIdSelectItems = findByCompanyIdSelectItem(companyId);
        if (ParamUtil.isBlank(companyIdSelectItems)) {
            return selectItemSer;
        }
        List<SelectItem<String, String>> selectItemsCom = serviceProviderService.selectItemsByIds(companyIdSelectItems);
        //获取差集
        return selectItemSer.stream().filter(item -> !selectItemsCom.contains(item)).collect(toList());
    }

    public void addRelevance(String companyId, String[] strings) {
        if (strings.length < 1) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, "请选择要绑定的服务商");
        }
        List<CompanyProviderBind> companyProviderBindList = new ArrayList<>(strings.length);
        for (String string : strings) {
            CompanyProviderBind companyProviderBind = new CompanyProviderBind();
            companyProviderBind.setCompanyId(companyId);
            companyProviderBind.setServiceProviderId(string);
            companyProviderBind.setTotalMoney(BigDecimal.ZERO);
            companyProviderBind.setCommission(BigDecimal.ZERO);
            companyProviderBind.setGrantMoney(BigDecimal.ZERO);
            companyProviderBind.setBalance(BigDecimal.ZERO);
            companyProviderBindList.add(companyProviderBind);
        }
        this.saveBatch(companyProviderBindList);
    }


    public CompanyProviderBind findByCompanyIdAndServiceProviderId(String companyId, String serviceProviderId) {
        return this.lambdaQuery()
                .eq(CompanyProviderBind::getCompanyId, companyId)
                .eq(CompanyProviderBind::getServiceProviderId, serviceProviderId)
                .one();

    }

}
